package me.learningandroid.ch2_7.uicomponentdemo;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.android.material.floatingactionbutton.FloatingActionButton;

public class Splash4Activity extends AppCompatActivity {
    private static final String BUNDLE_CURLAYOUTGRAVITY="curLayoutGravity";
    private static final String BUNDLE_CURGRAVITY="curGravity";

    private static final int[] GRAVITIES = {Gravity.CENTER_VERTICAL|Gravity.CENTER_HORIZONTAL, Gravity.CENTER_VERTICAL|Gravity.LEFT, Gravity.CENTER_VERTICAL|Gravity.RIGHT,
            Gravity.TOP|Gravity.LEFT, Gravity.TOP|Gravity.CENTER_HORIZONTAL, Gravity.TOP|Gravity.RIGHT,
            Gravity.BOTTOM|Gravity.LEFT, Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL, Gravity.BOTTOM|Gravity.RIGHT};
    private int curLayoutGravity = 0;
    private int curGravity = 0;

    public static void startActivity(Context context) {
        Intent intent = new Intent(context, Splash4Activity.class);
        context.startActivity(intent);
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash4);

        Button layoutGravityBtn = findViewById(R.id.layoutGravityBtn);
        layoutGravityBtn.setOnClickListener(v-> {
            curLayoutGravity = (curLayoutGravity+1)%GRAVITIES.length;
            // `layout_gravity`控制组件在layout中的位置
            // 以下是代码中设置`layout_gravity`的方法
            FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT);
            lp.gravity = GRAVITIES[curLayoutGravity];
            layoutGravityBtn.setLayoutParams(lp);
        });

        TextView gravityText = findViewById(R.id.gravityText);
        gravityText.setOnClickListener(v -> {
            curGravity = (curGravity+1)%GRAVITIES.length;
            // `gravity`控制组件中的内容（如文本）在组件内的位置
            // 以下是代码中设置`gravity`的方法
            gravityText.setGravity(GRAVITIES[curGravity]);
        });


        FloatingActionButton nextBtn = findViewById(R.id.nextBtn);
        nextBtn.setOnClickListener((view)-> {
            Splash5Activity.startActivity(this);
        });
    }

    @Override
    protected void onSaveInstanceState(@NonNull Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putInt(BUNDLE_CURLAYOUTGRAVITY,curLayoutGravity);
        outState.putInt(BUNDLE_CURGRAVITY,curGravity);
    }

    @Override
    protected void onRestoreInstanceState(@NonNull Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        curLayoutGravity = savedInstanceState.getInt(BUNDLE_CURLAYOUTGRAVITY);
        curGravity = savedInstanceState.getInt(BUNDLE_CURGRAVITY);
    }
}